package sunstar.com.xq.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import sunstar.com.xq.R
import sunstar.com.xq.databean.CardData
import sunstar.com.xq.util.ViewInitUtil

/**
 * Created by Sun.Star on 2017/6/19.
 */
class TimeLineAdapter(var context: Context, var mutableList: MutableList<CardData>) : RecyclerView.Adapter<TimeLineAdapter.TimeLineHolder>() {
    override fun onBindViewHolder(holder: TimeLineAdapter.TimeLineHolder?, position: Int) {
        holder!!.title.text = mutableList[position].cardTtitle
        holder!!.time.text = mutableList[position].createdAt

    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): TimeLineAdapter.TimeLineHolder {
        if (viewType == 0) {
            return TimeLineHolder(LayoutInflater.from(context).inflate(R.layout.left_time_line_item, parent, false))
        } else {
            return TimeLineHolder(LayoutInflater.from(context).inflate(R.layout.right_time_line_item, parent, false))
        }
    }

    override fun getItemCount(): Int = mutableList.size

    override fun getItemViewType(position: Int): Int {
        if (position % 2 == 1) {
            return 0
        } else {
            return 1
        }
    }


    class TimeLineHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val viewInit by lazy {
            ViewInitUtil(itemView)
        }
        val time by lazy {
            viewInit.getView<TextView>(R.id.time_line_time)
        }
        val title by lazy {
            viewInit.getView<TextView>(R.id.time_line_name)
        }
    }
}